package com.cya.cyaproducts;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class MainActivity extends Activity implements OnScrollListener{

	
    ListView listView;
    private int currentPage = 1;
    private Boolean statusLoadList = true;
    private ItemAdapter adapter;
    private int currentFirstVisibleItem;
    private int currentVisibleItemCount;
    private int currentTotalItemCount;
    private View loadingViewFooter;
    
    private Boolean isLoading = false;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Model.LoadModel(currentPage);
        ArrayList<Item> listItem = Model.Items;
        
        listView = (ListView) findViewById(R.id.listView);
        loadingViewFooter = getLayoutInflater().inflate(R.layout.loading_footer, null);
    	listView.addFooterView(loadingViewFooter);
        adapter = new ItemAdapter(this,R.layout.row, listItem);
        listView.setAdapter(adapter);
        listView.setOnScrollListener(this);
        
        listView.setOnItemClickListener(new OnItemClickListener() {
	        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
	        	int itemId = adapter.getIdItemByPosition(position);
	    		Bundle sendBundle = new Bundle();
	    		sendBundle.putString("id", Integer.toString(itemId));
	    		Intent intent = new Intent(MainActivity.this, DetailActivity.class);
	    		intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
	    		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
	    		intent.putExtras(sendBundle);
	    		startActivity(intent);
	    	}
        });

    }
    
    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        this.currentFirstVisibleItem = firstVisibleItem;
        this.currentVisibleItemCount = visibleItemCount;
        this.currentTotalItemCount = totalItemCount;
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
    	
    	
		if ((currentFirstVisibleItem + currentVisibleItemCount >= currentTotalItemCount-1) && statusLoadList) {
            if(!isLoading){
                 isLoading = true;
                 currentPage++;
                 //load more list items:
                 Model.LoadModel(currentPage);
                 ArrayList<Item> listItem = Model.Items;
                 if (listItem != null){
 	                for (int i = 0; i < listItem.size(); i++) {
 	                	Item itemss = listItem.get(i);
 	                	adapter.add(itemss);
 	            	}
 	               isLoading = false;
                 } else{
                 	statusLoadList = false;
                 }
            } else {
            	
            }
        }
		listView.removeFooterView(loadingViewFooter);
    }
    
    @Override
    public void onBackPressed() {
        
         AlertDialog.Builder builder = new AlertDialog.Builder(this);
         builder.setMessage("Are you sure you want to exit?")
	           .setCancelable(false)
	           .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
	               public void onClick(DialogInterface dialog, int id) {
	                    finish();
	               }
	           })
	           .setNegativeButton("No", new DialogInterface.OnClickListener() {
	               public void onClick(DialogInterface dialog, int id) {
	                    dialog.cancel();
	               }
	           });
	    AlertDialog alert = builder.create();
	    alert.show();
    }
    
}
